﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Lon.Util
{
    public class LonXmlNode
    {
        protected XmlNode node;
        public LonXmlNode(XmlNode node)
        {
            this.node = node;
        }
        /// <summary>
        /// 从属性中获取一个整数值
        /// </summary>
        /// <param name="attrName">属性值</param>
        /// <param name="defaultVal">获取失败时的默认值默认值</param>
        /// <returns></returns>
        public int GetInt(String attrName,int defaultVal)
        {
            try
            {
                String temp = node.Attributes[attrName].Value;
                return StringHelper.ToInt(temp, defaultVal);
            }
            catch
            {
                return defaultVal;
            }
        }

        static public int GetInt(XmlNode xNode, String attrName, int defaultVal)
        {
            try
            {
                String temp = xNode.Attributes[attrName].Value;
                return StringHelper.ToInt(temp, defaultVal);
            }
            catch
            {
                return defaultVal;
            }
        }
        /// <summary>
        /// 从属性中获取一个浮点值
        /// </summary>
        /// <param name="attrName">属性名称</param>
        /// <param name="defaultVal">读取失败时的默认值</param>
        /// <returns></returns>
        static public float GetFloat(XmlNode xNode, String attrName, float defaultVal)
        {
            try
            {
                String temp = xNode.Attributes[attrName].Value;
                return StringHelper.ToFloat(temp, defaultVal);
            }
            catch
            {
                return defaultVal;
            }
        }
        /// <summary>
        /// 从属性中获取一个浮点值
        /// </summary>
        /// <param name="attrName">属性名称</param>
        /// <param name="defaultVal">读取失败时的默认值</param>
        /// <returns></returns>
        public float GetFloat(String attrName, float defaultVal)
        {
            try
            {
                String temp = node.Attributes[attrName].Value;
                return StringHelper.ToFloat(temp, defaultVal);
            }
            catch
            {
                return defaultVal;
            }
        }

        /// <summary>
        /// 从属性中获取一个布尔值
        /// </summary>
        /// <param name="attrName">属性名称</param>
        /// <param name="defaultVal">读取失败时的默认值</param>
        /// <returns></returns>
        public bool GetBool(String attrName, bool defaultVal)
        {
            try
            {
                String temp = node.Attributes[attrName].Value;
                return StringHelper.ToBool(temp, defaultVal);
            }
            catch
            {
                return defaultVal;
            }
        }
        /// <summary>
        /// 从属性中获取一个字符串
        /// 获取失败时返回空
        /// </summary>
        /// <param name="attrName">属性名称</param>
        /// <returns></returns>
        static public String GetString(XmlNode node,String attrName)
        {
            try
            {
                XmlAttribute attr = node.Attributes[attrName];
                if (attr == null)
                {
                    return "";
                }
                String temp = node.Attributes[attrName].Value;
                return temp;
            }
            catch
            {
                return "";
            }
        }


        /// <summary>
        /// 从属性中获取一个字符串
        /// 获取失败时返回空
        /// </summary>
        /// <param name="attrName">属性名称</param>
        /// <returns></returns>
        public String GetString(String attrName)
        {
            try
            {
                String temp = node.Attributes[attrName].Value;
                return temp;
            }
            catch
            {
                return "";
            }
        }

        public List<LonXmlNode> GetSubNodes()
        {
            List<LonXmlNode> nodes = new List<LonXmlNode>();
            foreach (XmlNode subNode in node.ChildNodes)
            {
                LonXmlNode lonNode = new LonXmlNode(subNode);
                nodes.Add(lonNode);
            }
            return nodes;
        }
    }
}
